#!/usr/bin/python3

from telnetlib import Telnet
import re
import time

def preconfig(username, password, rackno, verno, devno):
    if rackno == 'BJRack01':
		rackip = '172.17.100.111'
	elif rackno == 'BJRack02':
		rackip = '172.17.100.112'
	elif rackno == 'BJRack03':
		rackip = '172.17.100.113'

	portno = 2000 + devno

	tn = Telnet(rackip, portno)

#正则表达式匹配各种状态
	yesorno = re.compile(r'.*Please answer.*')
	exc = re.compile(r'.*>')
	configure = re.compile(r'.*\(config.*\)#')
	priv = re.compile(r'.*#')
	clearconfigrouter = re.compile(r'Router(#|>)')
	clearconfigswitch = re.compile(r'Switch(#|>)')
	passwd = re.compile(r'Password: ')

#多次回车确认状态

	tn.write(b'\r\n')
	tn.read_very_eager()
	time.sleep(1)
	tn.write(b'\r\n')
	tn.read_very_eager()
	time.sleep(1)
	tn.write(b'\r\n')
	tn.read_very_eager()

#devno与设备名称的对应表

	if devno == 1:
		devname = 'R1'
	elif devno == 2:
		devname = 'R2'
	elif devno == 3:
		devname = 'R3'
	elif devno == 4:
		devname = 'R4'
	elif devno == 5:
		devname = 'R5'
	elif devno == 6:
		devname = 'R6'
	elif devno == 11:
		devname = 'SW1'
	elif devno == 12:
		devname = 'SW2'
	elif devno == 13:
		devname = 'SW3'
	elif devno == 14:
		devname = 'SW4'
	elif devno == 15:
		devname = 'SW5'
	elif devno == 16:
		devname = 'SW6'
#获取位置状态信息

	rackreply = tn.expect([],timeout=1)[2].decode().strip()

#基于状态采取行为
	while True:
		tn.write(b'\n')
		rackreply = tn.expect([],timeout=1)[2].decode().strip()
		if yesorno.match(rackreply):
			#yes or no初始化状态
			tn.write(b'no\n\n\n')
			time.sleep(5)
			tn.write(b'\n')
			while True:
				yesornoreply = tn.expect([],timeout=1)[2].decode().strip()
				if clearconfig.match(yesornoreply):
					break
				else:
					tn.write(b'\n')
			print('match yes or no')
			break

		elif configure.match(rackreply):
			#全局配置模式
			tn.write(b'\n')	
			tn.write(b'\r\n')	
			tn.write(b'end\n')
			time.sleep(1)
			tn.write(b'\n')
			tn.write(b'\r\n')
			tn.write(b'end\n')
			time.sleep(1)
			print('match configure')
			break

		elif exc.match(rackreply):
			#执行模式
			tn.write(b'\r\n')
			time.sleep(1)
			tn.write(b'\r\n')
			time.sleep(1)
			tn.write(b'en\n')
			enablereply = tn.expect([],timeout=1)[2].decode().strip()
			if passwd.match(enablereply):
				tn.write(b'cisco\n\n')
			tn.write(b'\n')
			print('match exec')
			break

		elif priv.match(rackreply):
			#特权模式
			tn.write(b'\n')	
			time.sleep(2)
			print('match priv')
			break
		else: 
			tn.write(b'\r\n')	
			time.sleep(1)
			tn.write(b'\r\n')	
			time.sleep(1)
			tn.write(b'\n')	
			time.sleep(1)
			print(rackno + ' ' + verno + ' ' + devname + ' next')

			

	tn.write(b'\r\n')
	tn.write(b'\n')
	tn.write(b'enable\n')
	time.sleep(1)
	tn.write(b'cisco\n\n')
	time.sleep(1)
	tn.write(b'enable\n')
	time.sleep(1)
	tn.write(b'cisco\n\n')
	time.sleep(1)
	tn.write(b'enable\n')
	time.sleep(1)
	tn.write(b'cisco\n\n')
	time.sleep(1)
	tn.write(b'enable\n')
	time.sleep(1)
	tn.write(b'cisco\n\n')

	if rackno == 'BJRack03' and devname == 'SW5':
		devname = '3SW5'
	preconfigfile = '/python/cgi-bin/preconfig/' + verno + '/' + devname + '.txt'
	preconfig = open(preconfigfile, 'rb')
	tn.write(preconfig.read())
	time.sleep(5)
	tn.write(b'end\n')
	time.sleep(1)
	tn.write(b'wr\n\n\n\n')
	preconfig.close()
	time.sleep(5)
	if rackno == 'BJRack03' and devname == 'SW5':
		devname = '3SW5'
	preconfigfile = '/python/cgi-bin/preconfig/' + verno + '/' + devname + '.txt'
	preconfig = open(preconfigfile, 'rb')
	tn.write(preconfig.read())
	time.sleep(5)
	tn.write(b'end\n')
	time.sleep(1)
	tn.write(b'wr\n\n\n\n')
	preconfig.close()
	time.sleep(5)
	if rackno == 'BJRack03' and devname == 'SW5':
		devname = '3SW5'
	preconfigfile = '/python/cgi-bin/preconfig/' + verno + '/' + devname + '.txt'
	preconfig = open(preconfigfile, 'rb')
	tn.write(preconfig.read())
	time.sleep(5)
	tn.write(b'end\n')
	time.sleep(1)
	tn.write(b'wr\n\n\n\n')
	preconfig.close()
	time.sleep(5)
#	if rackno == 'BJRack03' and devname == 'SW5':
#		devname = '3SW5'
#	preconfigfile = '/python/cgi-bin/preconfig/' + verno + '/' + devname + '.txt'
#	preconfig = open(preconfigfile, 'rb')
#	tn.write(preconfig.read())
#	time.sleep(1)
#	tn.write(b'end\n')
#	time.sleep(1)
#	tn.write(b'wr\n\n\n\n')
#	preconfig.close()	
	print(verno + ' ' + devname + ' configed')
	tn.close()
	print(tn.read_all())

if __name__ == "__main__":
	preconfig('labtest', 'Cisc0123', 'BJRack01', 'v12', 1)